﻿#include "mainwindow.h"
#include "myprocess.h"
#include "ui_mainwindow.h"
#include <formkeydata.h>
#include <formprinter.h>
#include <ui_formprinter.h>
#include <formprinterpack.h>
#include <QDebug>
#include <QListView>
#include <QFileDialog>
#include <QMessageBox>
#include "fun.hpp"
#include "myprinterfun.h"

MainWindow *obj=nullptr;
QTimer staticTimer;
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    obj = this;
    ui->setupUi(this);
    this->setWindowTitle(this->windowTitle()+getRandomString(2)+"["+APP_VERSION+"]");

    // 固定窗口大小
    //this->setMinimumSize(this->geometry().size());
    //this->setMaximumSize(this->geometry().size());

    this->setWindowTitle(APP_CONFIG);
    this->setAcceptDrops(true);
    formKeyData = new FormKeyData;
    ui->tabWidget->addTab(formKeyData, "数据初始化");
    formKeyData->show();
    formPrinter = new FormPrinter;
    formPrinter->show();
    ui->tabWidget->addTab(formPrinter, "铭牌/条码");
    connect(formKeyData, &FormKeyData::switch_tab_on, this, &MainWindow::switch_tab_on);
    formPrinterPack = new FormPrinterPack(formPrinter);
    formPrinterPack->show();
    ui->tabWidget->addTab(formPrinterPack, "外箱打印");
    // 获取全局配置
    formPrinter->bartend_exe = !getInfor("bartend_exe").isEmpty()?getInfor("bartend_exe"):formPrinter->bartend_exe;
    formPrinter->bylabel_exe = !getInfor("bylabel_exe").isEmpty()?getInfor("bylabel_exe"):formPrinter->bylabel_exe;
    QString static_str;
    static_str+=(QFile::exists(formPrinter->bartend_exe)?"":"<Bartend>");
    static_str+=(QFile::exists(formPrinter->bylabel_exe)?"":"<Bylabel>");
    if(!static_str.isEmpty()){
        ui->label_static->setText(static_str+"路径不正确！请检查并点击\"菜单\"重设！");
    }
    // 默认的导出数据格式
    connect(formPrinter, &FormPrinter::slot_reset_export_data_method, this, &MainWindow::slot_reset_export_data_method);
    connect(formPrinter, &FormPrinter::slot_reset_print_method, this, &MainWindow::slot_reset_print_method);
}


MainWindow::~MainWindow()
{
    delete ui;
}


// 调用打印
void MainWindow::ActionPrint(){
    QStringList arguments;
    arguments << "/F="+QDir::toNativeSeparators(QCoreApplication::applicationDirPath())+"\\xinfeng_print_case.btw" << "/P" << "/X";
    QProcess runexe(this);
    runexe.startDetached(formPrinter->bartend_exe, arguments);
}

// 日志
void MainWindow::log(QString str){
    QDir().mkpath("./logs");
    QFile f("./logs/pap_log"+QDateTime::currentDateTime().toString("yyyy-MM-dd")+".log");
    if(f.open(QIODevice::WriteOnly|QIODevice::Append)){
        QTextStream stream(&f);
        stream << QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss") << ":\n" << str << "\n";
        f.close();
    }
}

void MainWindow::static_text(QString str)
{
    obj->ui->label_static->setText(QDateTime::currentDateTime().toString("yy-MM-dd hh:mm:ss.")+str);
    // 2分钟后恢复
    // staticTimer.stop();
    // staticTimer.setInterval(120000);
    // connect(&staticTimer, &QTimer::timeout, obj, [=](){
    //     obj->ui->label_static->setText("");
    // });
    // staticTimer.start();
}



// 更新到列表中
void MainWindow::switch_tab_on(QString dataFilePath)
{
    ui->tabWidget->setCurrentWidget(formPrinter);
    formPrinter->switchCombox(dataFilePath);
}

// 切换tab窗口
void MainWindow::on_tabWidget_currentChanged(int index)
{
    if(ui->tabWidget->widget(index)==formPrinter){
        formPrinter->updateCombox();
    }
    if(ui->tabWidget->widget(index)==formPrinterPack){
        formPrinterPack->on_combobox_change(formPrinter->ui->comboBox->currentText());
    }
}

// 高级搜索
void MainWindow::on_actionsearch_all_triggered()
{
    ui->tabWidget->setCurrentWidget(formPrinter);
    formPrinter->SearchAll();
}


// 切换保存的模式的槽
void MainWindow::slot_reset_export_data_method(int ed_method)
{
    ui->actionexport_data_text->setChecked((bool)ed_method);
    ui->actionexport_data_xls->setChecked(!(bool)ed_method);
    ui->label_static2->setText((bool)ed_method?"数据格式:text，":"数据格式:xls，");
}
void MainWindow::on_actionexport_data_text_triggered(bool checked)
{
    ui->actionexport_data_text->setChecked(1);
    ui->actionexport_data_xls->setChecked(0);
    formPrinter->setDataMethod(1);
    ui->label_static2->setText("数据格式:text，");
}
void MainWindow::on_actionexport_data_xls_triggered(bool checked)
{
    ui->actionexport_data_xls->setChecked(1);
    ui->actionexport_data_text->setChecked(0);
    formPrinter->setDataMethod(0);
    ui->label_static2->setText("数据格式:xls，");
}

// 切换打印的模式的槽
void MainWindow::slot_reset_print_method(int ed_method)
{
    ui->actionmethod_bartender->setChecked(!(bool)ed_method);
    ui->actionmethod_bylabel->setChecked((bool)ed_method);
    ui->label_static3->setText((bool)ed_method?"打印方式:Bylabel":"打印方式:Bartender");
}
void MainWindow::on_actionmethod_bartender_triggered(bool checked)
{
    ui->actionmethod_bartender->setChecked(1);
    ui->actionmethod_bylabel->setChecked(0);
    formPrinter->setPrintMethod(0);
    ui->label_static3->setText("打印方式:Bartender");

}
void MainWindow::on_actionmethod_bylabel_triggered(bool checked)
{
    ui->actionmethod_bylabel->setChecked(1);
    ui->actionmethod_bartender->setChecked(0);
    formPrinter->setPrintMethod(1);
    ui->label_static3->setText("打印方式:Bylabel");
}

// 手动选择bartender程序路径
void MainWindow::on_actionreset_bartender_triggered()
{
    QString selectedFile = QFileDialog::getOpenFileName(this, "Open File", QDir::homePath(), "Bartender FilePath(*.exe);");
    if(!selectedFile.isEmpty()){
        formPrinter->bartend_exe = selectedFile;
        setInfor("bartend_exe", formPrinter->bartend_exe);
    }
    if(QFile::exists(formPrinter->bartend_exe)) ui->label_static->setText("");
}

// 自动获取bartender路径
void MainWindow::on_actionreset_bartend2_triggered()
{
    formPrinter->bartend_exe="";
    // 自动获取路径：bartender9.4,bartender2020,2021,2022;
    QStringList strList={"C:/Program Files (x86)/Seagull/BarTender Suite/bartend.exe",
                       "C:/Program Files/Seagull/BarTender Suite/bartend.exe",
                       "C:/Program Files/Seagull/BarTender 2022/BarTend.exe",
                       "C:/Program Files (x86)/Seagull/BarTender 2022/BarTend.exe",
                       "C:/Program Files/Seagull/BarTender 2021/BarTend.exe",
                       "C:/Program Files (x86)/Seagull/BarTender 2021/BarTend.exe",
                       "C:/Program Files/Seagull/BarTender 2020/BarTend.exe",
                        "C:/Program Files (x86)/Seagull/BarTender 2020/BarTend.exe"};
    foreach(auto str, strList){
        if(QFile::exists(str)){
            formPrinter->bartend_exe=str;
            QMessageBox::information(this, "提示", "找到bartender："+str);
            break;
        }
    }
    if(formPrinter->bartend_exe.isEmpty()){
        QMessageBox::warning(this, "错误", "未找到打印程序(bartender),请在\"菜单\"中手动设置.");
    }
    setInfor("bartend_exe", formPrinter->bartend_exe);
    if(QFile::exists(formPrinter->bartend_exe)) ui->label_static->setText("");
}

// 自动获取bylabel路径
void MainWindow::on_actionreset_bylabel_triggered()
{
    formPrinter->bylabel_exe="";
    // 自动获取路径：bartender9.4,bartender2020,2021,2022;
    QStringList strList={"C:/Program Files (x86)/BYLabel/BYLabel.exe",
                           "C:/Program Files/BYLabel/BYLabel.exe"};
    foreach(auto str, strList){
        if(QFile::exists(str)){
            formPrinter->bylabel_exe=str;
            QMessageBox::information(this, "提示", "找到bartender："+str);
            break;
        }
    }
    if(formPrinter->bylabel_exe.isEmpty()){
        QMessageBox::warning(this, "错误", "未找到打印程序(bartender),请在\"菜单\"中手动设置.");
    }
    setInfor("bylabel_exe", formPrinter->bylabel_exe);
    if(QFile::exists(formPrinter->bylabel_exe)) ui->label_static->setText("");
}

// 手动设置bylabel路径
void MainWindow::on_actionreset_bylabel2_triggered()
{
    QString selectedFile = QFileDialog::getOpenFileName(this, "Open File", QDir::homePath(), "Bartender FilePath(*.exe);");
    if(!selectedFile.isEmpty()){
        formPrinter->bylabel_exe = selectedFile;
        setInfor("bylabel_exe", formPrinter->bylabel_exe);
    }
    if(QFile::exists(formPrinter->bylabel_exe)) ui->label_static->setText("");
}


void MainWindow::on_actionforce_stop_process_triggered()
{
    formPrinter->runexe->kill();
    formPrinter->runexe->waitForFinished(5000);
}

